package com.yixin.hospital.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.VisitTemplateJobOffice;
import com.yixin.hospital.entity.dto.req.visitTemplate.VisitTemplateJobOfficeReqDTO;
import com.yixin.hospital.service.VisitTemplateJobOfficeService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/visit-template-job-office")
@Api(value = "就诊模板管理", tags = "就诊模板管理")
public class VisitTemplateJobOfficeController {

    @Resource
    VisitTemplateJobOfficeService visitTemplateJobOfficeService;


    @GetMapping("/info")
    @ApiOperation(value = "科室就诊模板详情")
    public ApiResult<List<Tree<Integer>>> info(int jobOfficeId) {
        return visitTemplateJobOfficeService.info(jobOfficeId);
    }

//    @GetMapping("/info")
//    @ApiOperation(value = "科室就诊模板详情")
//    public ApiResult info(int jobOfficeId) {
//        List<VisitTemplateJobOffice> templateCodeList = visitTemplateJobOfficeService.list(new LambdaQueryWrapper<VisitTemplateJobOffice>().eq(VisitTemplateJobOffice::getJobOfficeId, jobOfficeId));
//        if (CollectionUtil.isEmpty(templateCodeList)) return ApiResult.error(JOB_OFFICE_VISIT_TEMPLATE_NOT_FOUND);
//        Set<String> codeList = templateCodeList.stream().map(VisitTemplateJobOffice::getTemplateCode).collect(Collectors.toSet());
//        List<VisitTemplate> templateList = visitTemplateService.list(new LambdaQueryWrapper<VisitTemplate>().in(VisitTemplate::getTemplateCode, codeList));
//        TreeNodeConfig nodeConfig = new TreeNodeConfig();
//        List<Tree<Integer>> treeList = TreeUtil.build(templateList, 0, nodeConfig,
//                (node, tree) -> {
//                    tree.setId(node.getId().intValue());
//                    tree.setParentId(node.getPid().intValue());
//                    tree.setName(node.getName());
//                    tree.putExtra("templateCode", node.getTemplateCode());
//                    tree.putExtra("templateName", node.getTemplateName());
//                    tree.putExtra("level", node.getLevel());
//                    tree.putExtra("sort", node.getSort());
//                    tree.putExtra("value", "");
//                    tree.putExtra("isFocus", false);
//                });
//        Map<String, VisitInfoVO> codeMap = new HashMap<>();
//        for (Tree<Integer> integerTree : treeList) {
//            String code = String.valueOf(integerTree.get("templateCode"));
//            VisitInfoVO templateCode = codeMap.get(code);
//            if (Objects.isNull(templateCode)) {
//                templateCode = new VisitInfoVO();
//                templateCode.getVisitTemplateList().add(integerTree);
//                codeMap.put(code, templateCode);
//                continue;
//            }
//            templateCode.getVisitTemplateList().add(integerTree);
//        }
//        List<VisitInfoVO> visitInfoVOList = codeMap.keySet().stream().map(code -> {
//            VisitInfoVO visitInfoVO = codeMap.get(code);
//            visitInfoVO.setTemplateCode(String.valueOf(visitInfoVO.getVisitTemplateList().get(0).get("templateCode")));
//            visitInfoVO.setTemplateName(String.valueOf(visitInfoVO.getVisitTemplateList().get(0).get("templateName")));
//            return visitInfoVO;
//        }).collect(Collectors.toList());
//        return ApiResult.success(visitInfoVOList);
//    }

    @GetMapping("/list")
    @ApiOperation(value = "科室就诊模板列表")
    public ApiResult list() {
        return visitTemplateJobOfficeService.allList();
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增就诊管理类别")
    public ApiResult add(@Valid @RequestBody VisitTemplateJobOfficeReqDTO visitTemplateAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitTemplateJobOfficeService.add(visitTemplateAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "修改就诊管理类别")
    @Transactional
    public ApiResult edit(@Valid @RequestBody VisitTemplateJobOfficeReqDTO visitTemplateAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitTemplateJobOfficeService.edit(visitTemplateAddReqDTO, userName);
    }

    @DeleteMapping("/remove/{jobOfficeId}")
    @ApiOperation(value = "删除就诊管理类别")
    public ApiResult remove(@PathVariable int jobOfficeId) {
        visitTemplateJobOfficeService.remove(new LambdaUpdateWrapper<VisitTemplateJobOffice>().eq(VisitTemplateJobOffice::getJobOfficeId, jobOfficeId));
        return ApiResult.success();
    }

}
